package com.tracesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tracesystem.commons.Result;
import com.tracesystem.controller.form.DailyReportForm;
import com.tracesystem.controller.form.MonthlyReportForm;
import com.tracesystem.controller.form.RecordForm;
import com.tracesystem.controller.info.DailyReportInfo;
import com.tracesystem.controller.info.MonthlyReportInfo;
import com.tracesystem.dao.IssueBaseInfoDao;
import com.tracesystem.model.IssueBaseInfo;
import com.tracesystem.model.IssueWorkLog;
import com.tracesystem.dao.IssueWorkLogDao;
import com.tracesystem.service.IssueWorkLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tracesystem.util.CommonsUtils;
import com.tracesystem.util.WoniuCopyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  工时业务实现类
 * </p>
 *
 * @author Kevin Martin
 * @since 2022-03-23
 */
@Service
public class IssueWorkLogServiceImpl extends ServiceImpl<IssueWorkLogDao, IssueWorkLog> implements IssueWorkLogService {
    @Autowired
    IssueBaseInfoDao issueBaseInfoDao;
    /*
     * @Description:记录工时
     * @Author: JMOCOK
     * @param recordForm: 记录工时参数
     * @return: com.tracesystem.commons.Result
     **/
    @Override
    public Result addLogWork(RecordForm recordForm) {
        QueryWrapper<IssueWorkLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ISSUE_ID", recordForm.getIssueId());
        List<IssueWorkLog> issueWorkLogs = baseMapper.selectList(queryWrapper);
        IssueBaseInfo issueBaseInfo = issueBaseInfoDao.selectById(recordForm.getIssueId());
        issueBaseInfo.setDoneRatio(recordForm.getDoneRatio());
        issueBaseInfo.setActWorkHours(issueBaseInfo.getActWorkHours().add(recordForm.getWorkHours()));
        issueBaseInfo.setIssueState(recordForm.getLogState());
        if(null == issueWorkLogs){
            issueBaseInfo.setActStartTime(CommonsUtils.dateToLocalDate(new Date()));
        }
        //修改缺陷记录 工时 状态登数据
        issueBaseInfoDao.updateById(issueBaseInfo);
        IssueWorkLog issueWorkLog = WoniuCopyUtil.copyOne(recordForm, IssueWorkLog.class);
        issueWorkLog.setLogDate(CommonsUtils.dateToLocalDate(new Date()));
        baseMapper.insert(issueWorkLog);
        return Result.getInstance().setMsg("记录工时成功");
    }
    /*
     * @Description:查询日报表
     * @Author: JMOCOK
     * @param dailyReportForm: 日报表查询参数
     * @return: com.tracesystem.commons.Result<com.tracesystem.controller.info.DailyReportInfo>
     **/
    @Override
    public Result<DailyReportInfo> getDailyReport(DailyReportForm dailyReportForm) {
        List<DailyReportInfo> dailyReportInfos = baseMapper.selectDailyReport(dailyReportForm);
        return Result.getInstanceList(DailyReportInfo.class).setData(dailyReportInfos);
    }

    /*
     * @Description:查询月报表
     * @Author: JMOCOK
     * @param monthlyReportForm: 月报表查询参数
     * @return: com.tracesystem.commons.Result<com.tracesystem.controller.info.MonthlyReportInfo>
     **/
    @Override
    public Result<MonthlyReportInfo> getMonthlyReport(MonthlyReportForm monthlyReportForm){
        List<MonthlyReportInfo> monthlyReportInfos = baseMapper.selectMonthlyReport(monthlyReportForm);
        HashMap<String, MonthlyReportInfo> stringMonthlyReportInfoHashMap = new HashMap<>();
        for (MonthlyReportInfo monthlyReportInfo : monthlyReportInfos) {
            stringMonthlyReportInfoHashMap.put(monthlyReportInfo.getProjectName(), monthlyReportInfo);
        }
        Set<String> projectName = monthlyReportInfos.stream().map(rm -> rm.getProjectName()).collect(Collectors.toSet());
        Set<String> logDate = monthlyReportInfos.stream().map(rm -> rm.getLogDate()).collect(Collectors.toSet());
        List<MonthlyReportInfo> outinfos = new ArrayList<>();
        for (String s : projectName) {
            MonthlyReportInfo monthlyReportInfo1 = new MonthlyReportInfo();
            monthlyReportInfo1.setProjectName(s);
            outinfos.add(monthlyReportInfo1);
            int flag1 = outinfos.size() - 1;
            for (String s1 : logDate) {
                for (MonthlyReportInfo monthlyReportInfo : monthlyReportInfos) {
                    if (monthlyReportInfo.getProjectName().equals(s) && monthlyReportInfo.getLogDate().equals(s1)) {
                        int flag2 = outinfos.size() - 1;
                        MonthlyReportInfo monthlyReportInfo2 = new MonthlyReportInfo();
                        if (flag1 == flag2) {

                            monthlyReportInfo2.setLogDate(s1);

                        } else {
                            monthlyReportInfo2.setUsername(monthlyReportInfo.getUsername());
                            monthlyReportInfo2.setWorkHours(monthlyReportInfo.getWorkHours());
                        }
                        outinfos.add(monthlyReportInfo2);
                    }
                }
            }
        }
        return Result.getInstanceList(MonthlyReportInfo.class).setData(outinfos);
    }
}
